package com.ruoyi.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.business.domain.SysUserSign;
import com.ruoyi.business.service.ISysUserSignService;
import com.ruoyi.business.vo.SysUserSignVo;
import com.ruoyi.business.websocket.UserWebSocketController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 用户签名信息Controller
 *
 * @author Tellsea
 * @date 2025-03-25
 */
@Api(tags = "用户签名信息Controller")
@RestController
@RequestMapping("/business/userSign")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SysUserSignController extends BaseController {

    private final ISysUserSignService sysUserSignService;

    private final UserWebSocketController userWebSocketController;

    @Autowired
    private ISysUserService userService;

    @ApiOperation("查询用户签名信息列表")
    @GetMapping("/list")
    public TableDataInfo<SysUserSignVo> list(SysUserSignVo entity) {
        return sysUserSignService.queryList(entity);
    }

    @ApiOperation("查询用户签名信息所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(SysUserSignVo entity) {
        return AjaxResult.success("查询成功", sysUserSignService.queryAll(entity));
    }

    @ApiOperation("导出用户签名信息列表")
    @Log(title = "用户签名信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserSignVo entity) {
        List<SysUserSignVo> list = sysUserSignService.queryAll(entity);
        ExcelUtil<SysUserSignVo> util = new ExcelUtil<>(SysUserSignVo.class);
        util.exportExcel(response, list, "用户签名信息数据");
    }

    @ApiOperation("获取用户签名信息详细信息")
    @GetMapping(value = "/getInfo/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return AjaxResult.success("查询成功", sysUserSignService.queryById(userId));
    }

    @ApiOperation("是否包含护理科接口")
    @GetMapping(value = "/isHLDept/{userId}")
    public AjaxResult isHLDept(@PathVariable("userId") Long userId) {
        SysUser user = userService.selectUserById(userId);
        List<Long> registerDeptIdList = user.getRegisterDeptIdList();
        if(registerDeptIdList!=null && registerDeptIdList.contains(13L)){
            return AjaxResult.success("查询成功", true);
        }else{
            return AjaxResult.success("查询成功", false);
        }
    }

    @ApiOperation("新增用户签名信息")
    @Log(title = "用户签名信息", businessType = BusinessType.INSERT)
    @PostMapping("addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody SysUserSignVo entity) {
        SysUserSignVo sysUserSignVo = sysUserSignService.queryById(entity.getUserId());
        if(sysUserSignVo==null){
            return add(entity);
        }
        return edit(entity);
    }

    @ApiOperation("新增用户签名信息")
    @Log(title = "用户签名信息", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody SysUserSignVo entity) {
        Date nowDate = DateUtils.getNowDate();
        entity.setUpdateTime(nowDate);
        entity.setCreateTime(nowDate);
        boolean save = sysUserSignService.save(entity);
        userWebSocketController.OnMessage(String.valueOf(entity.getUserId()),entity.getUserSign());
        return toAjax(save);
    }

    @ApiOperation("修改用户签名信息")
    @Log(title = "用户签名信息", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody SysUserSignVo entity) {
        Date nowDate = DateUtils.getNowDate();
        entity.setUpdateTime(nowDate);
        QueryWrapper<SysUserSign> query = new QueryWrapper<>();
        query.eq("user_id",entity.getUserId());
        boolean update = sysUserSignService.update(entity, query);
        userWebSocketController.OnMessage(String.valueOf(entity.getUserId()),entity.getUserSign());
        return toAjax(update);
    }

}
